// Package coding_round_1 6_116
// @Title coding_round_1
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/3 21:14
// @Create  2021/9/3 21:14
package coding_round_1

import "fmt"

func ShowWNode(root *WNode) {
	if root == nil {
		return
	}
	fmt.Println(root.Val)
	if root.Next != nil {
		fmt.Println("其中左节点为: ", root.Next.Val)
	}
	ShowWNode(root.Left)
	ShowWNode(root.Right)
}

func ConnectWNode(root *WNode) *WNode {
	if root == nil {
		return nil
	}
	mQ := []*WNode{root}
	for len(mQ) > 0 {
		size := len(mQ)
		// 定义临时队列
		tQ := []*WNode{}
		for i := 0; i < size; i++ {
			node := mQ[0]
			mQ = mQ[1:]
			tQ = append(tQ, node) // 把当前节点扔到临时队列中
			if node.Left != nil {
				mQ = append(mQ, node.Left)
			}
			if node.Right != nil {
				mQ = append(mQ, node.Right)
			}
		}
		// 连接
		var tNode *WNode
		for len(tQ) > 0 {
			temp := tQ[len(tQ)-1]
			tQ = tQ[:len(tQ)-1]
			temp.Next = tNode
			tNode = temp
		}
	}
	return root
}
